package com.gengyun.gengyunacquistion.scheduled;

import com.gengyun.gengyunacquistion.service.IAcquisitionCollectHistoryService;
import com.gengyun.gengyunacquistion.service.IAcquisitionUavService;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.util.EntityUtils;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;
import java.net.URISyntaxException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Slf4j
@DisallowConcurrentExecution
public class DataUavMultispectralImage implements Job {


    @Autowired
    private IAcquisitionUavService acquisitionUavService;



    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        System.out.println("正在进行任务4");

        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

        // 初始化基本数据
        String lastTimestamp = acquisitionUavService.getLastTimestamp();
        String beginTimestamp = String.valueOf(Long.parseLong(lastTimestamp) + 1000L);



        System.out.println("lastTimestamp ====> "+lastTimestamp);
        System.out.println("beginTimestamp ====> "+beginTimestamp);
        String endTimestamp = String.valueOf(System.currentTimeMillis());

        if (Long.parseLong(endTimestamp) - Long.parseLong(beginTimestamp) > 2678400000L) {
            endTimestamp = String.valueOf(Long.parseLong(beginTimestamp) + 2678400000L);
        }

        System.out.println("begin ====> "+beginTimestamp);
        System.out.println("end ====> "+endTimestamp);



        log.info("数据接口获取数据开始 ====> "+sf.format(new Date()));

        /**
         * 下面是java爬虫的开始
         */
        PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();

        CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(cm).build();

        // 请求路径拼接
        URIBuilder uriBuilder = null;
        try {
            uriBuilder = new URIBuilder("https://api.simplanting.cn/indicator/hebau/uav-multispectral-image?");
            uriBuilder.setParameter("cropsRotationPlotID", "16978");
            uriBuilder.setParameter("startTimestamp", beginTimestamp);
            uriBuilder.setParameter("endTimestamp", endTimestamp);

        } catch (URISyntaxException e) {
            e.printStackTrace();
        }

        // Get请求
        HttpGet httpGet = null;
        try {
            httpGet = new HttpGet(uriBuilder.build());
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }

        // 设置请求头
        httpGet.setHeader("content-type", "application/json");
        httpGet.setHeader("Accept", "*/*");
        httpGet.setHeader("Cache-Control", "no-cache");
        httpGet.setHeader("PDS-ESP-APP-TOKEN", "6a3d382c08f04f47b4303592c9ddb483\n");

        CloseableHttpResponse response = null;

        try {
            response = httpClient.execute(httpGet);
            String content = EntityUtils.toString(response.getEntity(), "utf8");
         //   System.out.println(content);
//            System.out.println(jobExecutionContext.getScheduler().getCurrentlyExecutingJobs());
            // 执行语句处理json数据
            //  createJsonFile(content,"./test_Hourly.txt");
              acquisitionUavService.JsonHandler(content,endTimestamp);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (response != null) {
                try {
                    response.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        log.info("数据接口获取数据完毕 ====> "+sf.format(new Date()));
    }
}
